package es.us.lsi.aws.api.server.controllers;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;

import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.gson.Gson;

import es.us.lsi.aws.api.externalservices.TwitterHelper;
import es.us.lsi.aws.api.model.TwitterSocialAccount;
import es.us.lsi.aws.api.server.Controller;
import es.us.lsi.aws.api.server.dao.DAOSocialAccount;

public class AccountsController extends Controller {

	@Override
	public void processRequest(HttpServletRequest req, HttpServletResponse res) {

		String pathInfo = req.getPathInfo();
		String[] splitted = pathInfo.split("/");
		Gson gson = new Gson();

		if (splitted.length == 2) {

			if (req.getMethod().equals(HttpMethod.GET)) {
				try {
					res.getWriter().print(
							gson.toJson(DAOSocialAccount.getAllAccounts()));
				} catch (IOException e) {
					ExceptionController.processError(req, res, e);
				}
			} else if (req.getMethod().equals(HttpMethod.POST)) {
				String name = req.getParameter("name");
				String socialNetwork = req.getParameter("socialNetwork");
				if (socialNetwork.equals("Twitter")) {
					TwitterSocialAccount acc = new TwitterSocialAccount();
					acc.setName(name);
					acc.setSocialNetwork(socialNetwork);
					TwitterHelper tHelper = new TwitterHelper(acc);
					try {
						String url = tHelper.startOAuthProcess();
						req.getSession().setAttribute("twitter",
								tHelper.getTwitter());
						req.getSession().setAttribute("requestToken",
								tHelper.getRequestToken());

						res.sendRedirect(url);
					} catch (IOException e) {
						ExceptionController.processError(req, res, e);
					}
				} else if (socialNetwork.equals("Facebook")) {
					// TODO
				} else {
					res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
				}
			} else {
				res.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
			}

		} else if (splitted.length == 4 && splitted[2].equals("callbackurl")) {

			String socialNetwork = splitted[3];
			if (socialNetwork.equals("twitter")) {

				Twitter twitter = (Twitter) req.getSession().getAttribute(
						"twitter");
				RequestToken requestToken = (RequestToken) req.getSession()
						.getAttribute("requestToken");
				String verifier = req.getParameter("oauth_verifier");
				try {
					AccessToken accessToken = twitter.getOAuthAccessToken(
							requestToken, verifier);
					req.getSession().removeAttribute("requestToken");
					
					TwitterSocialAccount socialAcc = new TwitterSocialAccount();
					socialAcc.setName(twitter.getScreenName());
					socialAcc.setSocialNetwork("Twitter");
					socialAcc.setToken(accessToken.getToken());
					socialAcc.setTokenSecret(accessToken.getTokenSecret());
					
					DAOSocialAccount.addAccount(socialAcc);
					res.sendRedirect("/client");
				} catch (Exception e) {
					ExceptionController.processError(req, res, e);
				}

			} else if (socialNetwork.equals("facebook")) {
				// TODO
			} else {
				res.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
			}
		} else {
			res.setStatus(HttpServletResponse.SC_NOT_FOUND);
		}

	}
}
